package org.dromara.run.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.run.domain.bo.LevelBo;
import org.dromara.run.domain.vo.LevelVo;
import org.dromara.run.domain.Level;
import org.dromara.run.mapper.LevelMapper;
import org.dromara.run.service.ILevelService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 阳光跑等级Service业务层处理
 *
 * @author chl
 * @date 2024-08-30
 */
@RequiredArgsConstructor
@Service
public class LevelServiceImpl implements ILevelService {

    private final LevelMapper baseMapper;

    /**
     * 查询阳光跑等级
     */
    @Override
    public LevelVo queryById(Long levelId){
        return baseMapper.selectVoById(levelId);
    }

    /**
     * 查询阳光跑等级列表
     */
    @Override
    public TableDataInfo<LevelVo> queryPageList(LevelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Level> lqw = buildQueryWrapper(bo);
        Page<LevelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询阳光跑等级列表
     */
    @Override
    public List<LevelVo> queryList(LevelBo bo) {
        LambdaQueryWrapper<Level> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Level> buildQueryWrapper(LevelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Level> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getLevelId()!=null, Level::getLevelId, bo.getLevelId());
        lqw.like(StringUtils.isNotBlank(bo.getLevelName()), Level::getLevelName, bo.getLevelName());
        return lqw;
    }

    /**
     * 新增阳光跑等级
     */
    @Override
    public Boolean insertByBo(LevelBo bo) {
        Level add = MapstructUtils.convert(bo, Level.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLevelId(add.getLevelId());
        }
        return flag;
    }

    /**
     * 修改阳光跑等级
     */
    @Override
    public Boolean updateByBo(LevelBo bo) {
        Level update = MapstructUtils.convert(bo, Level.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Level entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除阳光跑等级
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
